import Vue from 'vue'
import VueRouter from 'vue-router'

//获取原型对象上的push函数
const originalPush = VueRouter.prototype.push
//修改原型对象中的push方法
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

import Login from '@/views/login/Login.vue'
import Layout from '@/views/layout/Layout.vue'
Vue.use(VueRouter)

/* 配置路由*/
const routes = [
  /* 登录*/
  {
    path: '/login',
    component: Login,
  },
  /* 后台首页*/
  {
    path: '/',
    component: Layout,
    meta: { title: '后台首页', path: '/home' },
    redirect: '/home',
    children: [
      {
        path: '/home',
        component: () => import('@/views/home/Home.vue'),
      },
    ],
  },
  /* 订单管理*/
  {
    path: '/order',
    component: Layout,
    meta: { title: '订单管理', path: '/order' },
    children: [
      {
        path: '',
        component: () => import('@/views/order/Order.vue'),
      },
      {
        path: '/order/order-edit',
        component: () => import('@/views/order/OrderEdit.vue'),
      },
    ],
  },
  /* 商品管理*/
  {
    path: '/goods',
    component: Layout,
    meta: { title: '商品管理', path: '/goods' },
    redirect: '/goods/goods-list',
    children: [
      {
        meta: { title: '商品列表', path: '/goods/goods-list' },
        path: '/goods/goods-list',
        component: () => import('@/views/goods/GoodsList.vue'),
      },
      {
        meta: { title: '商品添加', path: '/goods/goods-add' },
        path: '/goods/goods-add',
        component: () => import('@/views/goods/GoodsAdd.vue'),
      },
      {
        meta: { title: '商品分类', path: '/goods/goods-cate' },
        path: '/goods/goods-cate',
        component: () => import('@/views/goods/GoodsCate.vue'),
      },
      {
        path: '/goods/goods-edit',
        component: () => import('@/views/goods/GoodsEdit.vue'),
      },
    ],
  },
  /* 店铺管理*/
  {
    path: '/shop',
    component: Layout,
    meta: { title: '店铺管理', path: 'shop' },
    children: [
      {
        path: '',
        component: () => import('@/views/shop/Shop.vue'),
      },
    ],
  },
  /*帐号管理*/
  {
    path: '/account',
    meta: { title: '账号管理', path: '/account' },
    component: Layout,
    redirect: '/account/account-list',
    children: [
      {
        meta: { title: '账号列表', path: '/account/account-list' },
        path: '/account/account-list',
        component: () => import('@/views/account/AccountList.vue'),
      },
      {
        meta: { title: '账号添加', path: '/account/account-add' },
        path: '/account/account-add',
        component: () => import('@/views/account/AccountAdd.vue'),
      },
      {
        meta: { title: '修改密码', path: '/account/password-modify' },
        path: '/account/password-modify',
        component: () => import('@/views/account/PasswordModify.vue'),
      },
      {
        meta: { title: '个人中心', path: '/account/person' },
        path: '/account/person',
        component: () => import('@/views/account/Person.vue'),
      },
    ],
  },
  /*销售统计*/
  {
    path: '/total',
    component: Layout,
    meta: { title: '销售统计', path: '/total' },
    redirect: '/total/total-goods',
    children: [
      {
        path: '/total/total-goods',
        meta: { title: '商品统计', path: '/total/total-goods' },
        component: () => import('@/views/total/TotalGoods.vue'),
      },
      {
        path: '/total/total-order',
        meta: { title: '订单统计', path: '/total/total-order' },
        component: () => import('@/views/total/TotalOrder.vue'),
      },
    ],
  },
]
const router = new VueRouter({
  routes,
})

// 配置全局路由导航前置守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('t_k')
  if (token) {
    next()
  } else {
    if (to.path === '/login') {
      next()
    } else {
      next({ path: '/login' })
    }
  }
})
export default router
